package br.com.gp.inicializador;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import br.com.gp.entidade.EntidadePadrao;
import br.com.gp.hibernate.HibernateUtilitario;

public abstract class InicializadorPadrao {
	private HibernateUtilitario hib;
	private Session session;
	private Transaction transacao;
	public HibernateUtilitario getHib() {
		return hib;
	}
	public <T extends EntidadePadrao> EntidadePadrao obterEntidadeForId(Class<T> obj,Integer id){
		Object query = getSession().load(obj, id);
		Hibernate.initialize(query);
		return (EntidadePadrao)query;
	}
	public void setHib(HibernateUtilitario hib) {
		this.hib = hib;
	}
	
	@SuppressWarnings("static-access")
	public Session getSession() {
		if(session ==null){
			setSession(hib.getSessionFactory().openSession());
		}
		return session;
	}
	public void setSession(Session session) {
		this.session = session;
	}
	public Transaction getTransacao() {
		setTransacao(getSession().beginTransaction());
		return transacao;
	}
	public void setTransacao(Transaction transacao) {
		this.transacao = transacao;
	}
	public void comit(){
		getTransacao().commit();
	}
	public void rowback(){
		getTransacao().rollback();
	}
	public void close(){
		getSession().close();
	}
	public abstract Object inicializar(Object obj);
}
